-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix issue where OrderInventory creates superfluous InventoryUnits #1751
Fix issue where OrderInventory creates superfluous InventoryUnits #1751
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 🎉
Fixed a second issue while investigating this. Previously, when the inventory count reached This could later cause an |
d910511
to
e0fef9f
Compare
This fixes an issue where OrderInventory would miscount the number of existing inventory_units on a line item and create extra records. InventoryUnits exist through the line_item.inventory_units, shipment.inventory_units, and order.inventory_units associations. Because of this, their associations can't be kept up to date in memory. OrderInventory was counting inventory using inventory_units.size, which would return the in-memory count if the line_items.inventory_units association was loaded. This issue was exposed by adding to the same line item mutliple times without reloading the order. The line_item.inventory_units association would be become `loaded?`, because ActiveRecord's `#size` marks a record as loaded if the count returns 0 (TIL!). On the next pass it would see the wrong size because the association was out of date, and add two inventory units instead of one. This avoids the issue by counting inventory using inventory_units.count, which doesn't consider the in-memory association.
e0fef9f
to
1ae03b4
Compare
This fixes an issue where OrderInventory would miscount the number of existing inventory_units on a line item and create extra records.
InventoryUnits exist through the line_item.inventory_units, shipment.inventory_units, and order.inventory_units associations. Because of this, their associations can't be kept up to date in memory.
OrderInventory was counting inventory using inventory_units.size, which would return the in-memory count if the line_items.inventory_units association was loaded.
This issue was exposed by adding to the same line item multiple times without reloading the order. The line_item.inventory_units association would be become
loaded?
, because ActiveRecord's#size
marks a record as loaded if the count returns 0 (TIL!). On the next pass it would see the wrong size because the association was out of date, and add two inventory units instead of one.This avoids the issue by counting inventory using inventory_units.count, which doesn't consider the in-memory association.